//
//  AnotherView.m
//  MMLOOK
//
//  Created by dllo on 15/9/5.
//  Copyright (c) 2015年 武俊娥. All rights reserved.
//

#import "AnotherView.h"
#import "AsynConnect.h"
#import "AnotherCollectionViewCell.h"
#import "MJRefresh.h"
#import "MBProgressHUD.h"

@interface AnotherView ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionView *colection;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, retain) MBProgressHUD *mbProView;

@end

@implementation AnotherView

- (void)dealloc
{
    [_mbProView release];
    [_viewArray release];
    [_colection release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.page = 1;
        self.viewArray = [NSMutableArray array];
        [self creatCollection];
        [self anotherConnection];
        [self head];
    }
    return self;
}

- (void)creatCollection
{
    UICollectionViewFlowLayout *layout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.itemSize = CGSizeMake((self.frame.size.width - 30) / 2, (self.frame.size.width - 30) / 2 + 50);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.colection = [[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:layout] autorelease];
    _colection.dataSource = self;
    _colection.delegate = self;
    _colection.backgroundColor = [UIColor whiteColor];
    [_colection registerClass:[AnotherCollectionViewCell class] forCellWithReuseIdentifier:@"another"];
    [self addSubview:_colection];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (_viewArray.count == 0) {
        
        return  0;
    }
    
    return _viewArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    AnotherCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"another" forIndexPath:indexPath];
    NSMutableDictionary *dic = [_viewArray objectAtIndex:indexPath.row];
    
    [cell sendDic:dic];
    
    return cell;
}

- (void)anotherConnection
{
    [self createMbProView];
    
    [AsynConnect asynConnectWithUrlstr:[NSString stringWithFormat:@"http://guang.isee.com.cn/homepage/ajax/get_album_source.json?current_page=%lu&cursor=&t=1369472356&user_id=792936011&album_id=3000352275&page=1&type=0", self.page] parmater:nil block:^(NSData *data) {
        
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableDictionary *littledic = [dic objectForKey:@"data"];
        id arr = [littledic objectForKey:@"data"];
        
        if ([arr isKindOfClass:[NSArray class]]) {
            
            arr = (NSArray *)arr;
            
            for (NSMutableDictionary *dic in arr) {
                
                [self.viewArray addObject:dic];
            }
        } else {
            
            [_colection.footer noticeNoMoreData];
        }
        
        [_colection reloadData];
        [_mbProView hide:YES];
    }];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate Anortherjump:[_viewArray objectAtIndex:indexPath.item]];
}

#pragma mark 下拉刷新、上拉加载
- (void)head
{
    // 下拉刷新
    _colection.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [_colection reloadData];// 刷新数据
        [_colection.header endRefreshing];// 结束刷新
        [_colection.footer resetNoMoreData];
    }];
    
    // 上拉加载
    _colection.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        _page++;
        [self anotherConnection];// 请求新数据
        [_colection reloadData];// 刷新数据
        [_colection.footer endRefreshing];// 结束加载
    }];
    _colection.footer.hidden = YES;// 默认先隐藏footer
}

- (void)createMbProView
{
    self.mbProView = [[[MBProgressHUD alloc] initWithView:self] autorelease];
    _mbProView.mode = MBProgressHUDModeIndeterminate;// 样式 菊花
    _mbProView.labelText = @"加载中. . .";
    [_mbProView show:YES];
    [self addSubview:_mbProView];
    [_mbProView hide:YES afterDelay:20];
}

@end
